import { queryAll } from "../../database/dbUtils";
//获取数据
const getDataList = async (ctx: any) => {
  let sql = `select * from ssxiangq`;
  let res: any = await queryAll(sql);
  let data = res.map((item: any) => {
    return {
      id: item.id,
      sid: item.sid,
      name: item.name,
      college: item.college,
      DormitoryNo: item.DormitoryNo,
      dorm_number: item.dorm_number,
      max_number: item.max_number,
      money: item.money,
      now_number: item.now_number,
      evaluate: item.evaluate,
      count: item.count,
      c_count: item.c_count,
      person: JSON.parse(item.person),
    };
  });
  if (ctx.query.val === '空值') {
    ctx.status = 200;
    ctx.body = [data[0]];
  } else {
    let obj = data.filter((item: any) => {
      if (item.college === ctx.query['1']) {
        return item.sid.indexOf(ctx.query['0']) > -1
      }
    })
    ctx.status = 200;
    ctx.body = obj;
  }
};
//值日生表
const zhirishang = async (ctx: any) => {
  let { keyPath, keyPath1, val, inset, id } = ctx.query;
  let sql = `select  * from ssxiangq`;
  let res: any = await queryAll(sql);
  let data = res.map((item: any) => {
    return {
      id: item.id,
      sid: item.sid,
      name: item.name,
      college: item.college,
      DormitoryNo: item.DormitoryNo,
      dorm_number: item.dorm_number,
      max_number: item.max_number,
      money: item.money,
      now_number: item.now_number,
      evaluate: item.evaluate,
      count: item.count,
      c_count: item.c_count,
      person: JSON.parse(item.person)
    };
  })
  let arr = data.filter((item: any) => {
    if (item.id === id) {
      return item.person.filter((item: any) => {
        if (item.timer === inset) {
          item.timer = JSON.parse(val).timer
        }
        if (item.id === JSON.parse(val).id) {
          item.timer = inset
        }
      })
    }
  })


arr.forEach(async (item: any) => {
    let sqll = `UPDATE ssxiangq SET  person='${JSON.stringify(item.person)}' where id="${item.id}"`;
    await queryAll(sqll);
  })
ctx.status = 200;
ctx.body = arr;
}
//获取所有的数据
const getDormitoryList = async (ctx: any) => {
  let sql = `select * from dormitory`;
  let res: any = await queryAll(sql);
  ctx.status = 200;
  ctx.body = {
    code: 200,
    msg: '获取成功',
    data: res
  }
}
//修改评级
const xougaipingji = async (ctx: any) => {
  let { e, val } = ctx.query;
  console.log(e, val);
  let sql = `select  * from ssxiangq`;
  let res: any = await queryAll(sql);
  let data = res.map((item: any) => {
    return {
      id: item.id,
      sid: item.sid,
      name: item.name,
      college: item.college,
      DormitoryNo: item.DormitoryNo,
      dorm_number: item.dorm_number,
      max_number: item.max_number,
      money: item.money,
      now_number: item.now_number,
      evaluate: item.evaluate,
      count: item.count,
      c_count: item.c_count,
      person: JSON.parse(item.person)
    };
  })
  let arr1 = data.filter((item: any) => {
    return item.id === val
  })
  let arr = arr1.filter((item: any) => {
    item.evaluate = e;
    if (e === '差') {
      let c_count = JSON.parse(item.c_count);
      c_count + 1;
      item.c_count = c_count
    } else {
      let count = JSON.parse(item.count);
      count + 1;
      item.count = count
    }
    return item.person.filter((item: any) => {
      return {
        id: item.id,
        name: item.name,
        timer: item.timer,
        falgboolean: item.falgboolean,
        classParser: item.classParser,
        banji: item.banji
      }
    })
  })
  arr.forEach(async (item: any) => {
    let sqll = `UPDATE ssxiangq SET evaluate='${item.evaluate}',count='${item.count}',c_count='${item.c_count}' where id="${item.id}"`;
    await queryAll(sqll);
  })
  ctx.status = 200;
  ctx.body = arr
}
//修改宿舍长
const xgshopdata = async (ctx: any) => {
  let { keyPath, keyPath1, id, val } = ctx.query;
  console.log(keyPath, keyPath1, id, val);

  let sql = `select  * from ssxiangq`;
  let res: any = await queryAll(sql);
  let data = res.map((item: any) => {
    return {
      id: item.id,
      sid: item.sid,
      name: item.name,
      college: item.college,
      DormitoryNo: item.DormitoryNo,
      dorm_number: item.dorm_number,
      max_number: item.max_number,
      money: item.money,
      now_number: item.now_number,
      evaluate: item.evaluate,
      count: item.count,
      c_count: item.c_count,
      person: JSON.parse(item.person)
    };
  })
  let arr = data.filter((item: any) => {
    if (item.id === id) {
      return item.person.filter((item: any) => {
        if (item.falgboolean === true) {
          item.falgboolean = false;
          item.timer = JSON.parse(val).timer;
        }
        if (item.id === JSON.parse(val).id) {
          item.falgboolean = true;
          item.timer = null
        }
      })

    }
  })
  arr.forEach(async (item: any) => {
    console.log(item.person);
    let sqll = `UPDATE ssxiangq SET  person='${JSON.stringify(item.person)}' where id="${item.id}"`;
    await queryAll(sqll);
  })
  ctx.status = 200;
  ctx.body = arr
}
//移除宿舍名单
const remove = async (ctx: any) => {
  let { keyPath, keyPath1, id, val } = ctx.query;
  console.log(keyPath, keyPath1, id, val);

  let sql = `select  * from ssxiangq`;
  let res: any = await queryAll(sql);
  let data = res.map((item: any) => {
    return {
      id: item.id,
      sid: item.sid,
      name: item.name,
      college: item.college,
      DormitoryNo: item.DormitoryNo,
      dorm_number: item.dorm_number,
      max_number: item.max_number,
      money: item.money,
      now_number: item.now_number,
      evaluate: item.evaluate,
      count: item.count,
      c_count: item.c_count,
      person: JSON.parse(item.person)
    };
  })
  let arr = data.filter((item: any) => {
    if (item.id === id) {
      return item.person.filter((item: any) => {
        if(item.id===JSON.parse(val).id){
          item.name='';
          item.banji='';
        }
      })
    }
  })
  arr.forEach(async (item: any) => {
    let sqll = `UPDATE ssxiangq SET  person='${JSON.stringify(item.person)}' where id="${item.id}"`;
    await queryAll(sqll);
  })
  ctx.status = 200;
  ctx.body = arr
}
//替换宿舍名单
const tihuan = async (ctx: any) => {
  let { keyPath, keyPath1, id, val } = ctx.query;
  console.log(keyPath, keyPath1, id, val);
  let sql = `select  * from ssxiangq`;
  let res: any = await queryAll(sql);
  let data = res.map((item: any) => {
    return {
      id: item.id,
      sid: item.sid,
      name: item.name,
      college: item.college,
      DormitoryNo: item.DormitoryNo,
      dorm_number: item.dorm_number,
      max_number: item.max_number,
      money: item.money,
      now_number: item.now_number,
      evaluate: item.evaluate,
      count: item.count,
      c_count: item.c_count,
      person: JSON.parse(item.person)
    };
  })
  let arr = data.filter((item: any) => {
    if (item.id === id) {
      return item.person.filter((item: any) => {
        if(item.id===1){
          item.id=`${JSON.parse(val).id}`;
        }
        if(item.id===JSON.parse(val).id){
          item.id=1;
        }
      })
    }
  })
  arr.forEach(async (item: any) => {
    console.log(item.person);
    // let sqll = `UPDATE ssxiangq SET  person='${JSON.stringify(item.person)}' where id="${item.id}"`;
    // await queryAll(sqll);
  })
  ctx.status = 200;
  ctx.body = arr
}
//添加宿舍
const addDormitory = async (ctx: any) => {
  const { college, campus, dorm_number, DormitoryNo, max_number, money } = ctx.request.body;
  let all = `select * from dormitory where college ="${college}"`;
  let list: any = await queryAll(all);
  let arr = JSON.parse(list[0].campus);
  arr.forEach((item: any) => {
    if (item.name === campus) {
      item.dormList.push({
        id: item.dormList.length + 1,
        DormitoryNo: DormitoryNo,
        dorm_number: dorm_number,
        max_number: max_number,
        money: money,
        now_number: 0,
        evaluate: '',
        count: '',
        c_count: '',
        person: []
      });
    }
  });
  let update = await queryAll(`update dormitory set campus=${JSON.stringify(arr)} where college='${college}'`);
  if (update) {
    let data = await queryAll(`select * from dormitory`);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      msg: "获取成功",
      data: data,
    };
  }
};
//导入数据
const importDate = async (ctx: any) => {
  const { addList } = ctx.request.body;
  let result: any = await queryAll(`select * from dormitory`);
  addList.forEach((item: any) => {
    result.forEach(async (ite: any) => {
      if (ite.college === item.college) {
        JSON.parse(ite.campus).forEach((it: any) => {
          if (it.name === item.campus) {
            it.dormList.push({
              id: item.dormList.length + 1,
              DormitoryNo: item.DormitoryNo,
              dorm_number: item.dorm_number,
              max_number: item.max_number,
              money: item.money,
              now_number: 0,
              evaluate: '',
              count: '',
              c_count: '',
              person: []
            });
          }
        });
        await queryAll(`update dormitory set campus=${JSON.stringify(item.campus)} where college='${item.college}'`)
      }
    })
  })
  let data = await queryAll(`select * from dormitory`);
  ctx.status = 200;
  ctx.body = {
    code: 200,
    msg: "获取成功",
    data: data,
  };
}
export { getDataList, getDormitoryList, addDormitory, importDate, xgshopdata, zhirishang, xougaipingji,remove,tihuan };

